package fr.blogger.ejb.model;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.eclipse.persistence.annotations.Index;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name = "utilisateur")
@TableGenerator(name = "SEQUENCE_UTILISATEUR", initialValue = 0, table = "sequence", pkColumnName = "code", valueColumnName = "valeur", allocationSize = 1)
public class Utilisateur implements UserDetails {

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "SEQUENCE_UTILISATEUR")
	private Integer id;

	@Index
	@Size(max = 255)
	@NotNull
	@Column(name = "mail", length = 255, nullable = false)
	private String mail;

	@Size(max = 128)
	@NotNull
	@Column(name = "password", length = 128, nullable = false)
	private String password;

	@Index
	@Size(max = 50)
	@NotNull
	@Column(name = "nom", length = 50, nullable = false)
	private String nom;

	@Index
	@Size(max = 50)
	@NotNull
	@Column(name = "prenom", length = 50, nullable = false)
	private String prenom;

	@Size(max = 20)
	@Column(name = "token_modification_password", length = 20, nullable = true)
	private String tokenModificationPassword;

	@NotNull
	@Column(name = "nombre_echec_connexion", nullable = false)
	private int nombreEchecConnexion;

	@NotNull
	@Column(name = "actif", nullable = false)
	private boolean actif;

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_creation", nullable = false)
	private Date dateCreation;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_modification_password", nullable = true)
	private Date dateModificationPassword;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_derniere_connexion", nullable = true)
	private Date dateDerniereConnexion;

	@NotNull
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
	@JoinColumn(name = "ref_profil", nullable = false)
	private Profil profil;

	@OneToMany(cascade = CascadeType.REFRESH, mappedBy = "utilisateur")
	public List<News> listeNews;

	@OneToMany(cascade = CascadeType.REFRESH, mappedBy = "utilisateurCreateur")
	public List<Blog> listeBlog;

	@ManyToMany(cascade = {CascadeType.REMOVE, CascadeType.MERGE})
	@JoinTable(name = "suivi_blog", joinColumns = @JoinColumn(name = "ref_utilisateur", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "ref_blog", referencedColumnName = "id"))
	private List<Blog> listeBlogAbonne;

	public Utilisateur() {
		actif = true;
	}

	public String getNomPrenom() {
		return String.format("%s %s", nom, prenom);
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return profil.getListeRole();
	}

	@Override
	public String getUsername() {
		return mail;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return actif;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getTokenModificationPassword() {
		return tokenModificationPassword;
	}

	public void setTokenModificationPassword(String tokenModificationPassword) {
		this.tokenModificationPassword = tokenModificationPassword;
	}

	public Date getDateCreation() {
		return dateCreation;
	}

	public void setDateCreation(Date dateCreation) {
		this.dateCreation = dateCreation;
	}

	public Date getDateModificationPassword() {
		return dateModificationPassword;
	}

	public void setDateModificationPassword(Date dateModificationPassword) {
		this.dateModificationPassword = dateModificationPassword;
	}

	public Date getDateDerniereConnexion() {
		return dateDerniereConnexion;
	}

	public void setDateDerniereConnexion(Date dateDerniereConnexion) {
		this.dateDerniereConnexion = dateDerniereConnexion;
	}

	public Profil getProfil() {
		return profil;
	}

	public void setProfil(Profil profil) {
		this.profil = profil;
	}

	public List<News> getListeNews() {
		return listeNews;
	}

	public void setListeNews(List<News> listeNews) {
		this.listeNews = listeNews;
	}

	public List<Blog> getListeBlog() {
		return listeBlog;
	}

	public void setListeBlog(List<Blog> listeBlog) {
		this.listeBlog = listeBlog;
	}

	public List<Blog> getListeBlogAbonne() {
		return listeBlogAbonne;
	}

	public void setListeBlogAbonne(List<Blog> listeBlogAbonne) {
		this.listeBlogAbonne = listeBlogAbonne;
	}

	public boolean isActif() {
		return actif;
	}

	public void setActif(boolean actif) {
		this.actif = actif;
	}

	public int getNombreEchecConnexion() {
		return nombreEchecConnexion;
	}

	public void setNombreEchecConnexion(int nombreEchecConnexion) {
		this.nombreEchecConnexion = nombreEchecConnexion;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		Utilisateur other = (Utilisateur) obj;
		return new EqualsBuilder().append(id, other.id).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(id).toHashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id).toString();
	}

}
